package org.jeecg.modules.htxincailiao.position.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.CommonAPI;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.about.entity.HTContactUs;
import org.jeecg.modules.htxincailiao.account.entity.HTMerchant;
import org.jeecg.modules.htxincailiao.account.service.IHTMerchantService;
import org.jeecg.modules.htxincailiao.area.entity.HTArea;
import org.jeecg.modules.htxincailiao.area.service.IHTAreaService;
import org.jeecg.modules.htxincailiao.forums.entity.HTForumsQuestion;
import org.jeecg.modules.htxincailiao.position.entity.HTPosition;
import org.jeecg.modules.htxincailiao.position.entity.HTPositionCategory;
import org.jeecg.modules.htxincailiao.position.service.IHTPositionCategoryService;
import org.jeecg.modules.htxincailiao.position.service.IHTPositionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.permissions.HTRolePermission;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * @Description: ht_position
 * @Author: jeecg-boot
 * @Date:   2021-11-02
 * @Version: V1.0
 */
@Api(tags="ht_position")
@RestController
@RequestMapping("/position/hTPosition")
@Slf4j
public class HTPositionController extends JeecgController<HTPosition, IHTPositionService> {
	@Resource
	private IHTPositionService hTPositionService;

	@Resource
	private IHTMerchantService ihtMerchantService;

	@Resource
	private IHTAreaService areaService;

	@Lazy
	@Resource
	private CommonAPI commonApi;

	@Resource
	private IHTPositionCategoryService positionCategoryService;

	@AutoLog(value = "ht_position-分页列表查询")
	@ApiOperation(value="ht_position-分页列表查询", notes="ht_position-分页列表查询")
	@RequestMapping(value = "/pc-home-list", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> pcHomeList(HTPosition hTPosition,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		pageSize = 25;
		hTPosition.setIsHomeRecommended(1);
		hTPosition.setPositionStatus(0);
		return this.queryPageList(hTPosition, pageNo, pageSize, req);
	}

	/**
	 * 分页列表查询
	 *
	 * @param hTPosition
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "ht_position-分页列表查询")
	@ApiOperation(value="ht_position-分页列表查询", notes="ht_position-分页列表查询")
	@RequestMapping(value = "/list", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryPageList(HTPosition hTPosition,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				hTPosition= JSONObject.parseObject(line,HTPosition.class);
				pageNo = hTPosition.getPageNo();
				pageSize =hTPosition.getPageSize();
			}
		}
		if(oConvertUtils.isNotEmpty(hTPosition) &&
				oConvertUtils.isNotEmpty(hTPosition.getCategoryId()) &&
				hTPosition.getCategoryId().equals("0")) {
			//全部分类
			hTPosition.setCategoryId(null)	;
		}
		HTRolePermission.AddPositionPermission(req, hTPosition);
		QueryWrapper<HTPosition> queryWrapper = QueryGenerator.initQueryWrapper(hTPosition, req.getParameterMap());

		Page<HTPosition> page = new Page<HTPosition>(pageNo, pageSize);
		IPage<HTPosition> pageList = hTPositionService.page(page, queryWrapper);
		List<HTPosition> list = pageList.getRecords();
		for (HTPosition position: list) {
			HTPositionCategory category = positionCategoryService.getById(position.getCategoryId());
			if(oConvertUtils.isNotEmpty(category) && oConvertUtils.isNotEmpty(category.getName( ))){
				position.setCategoryName(category.getName());
			}

			HTArea provinceArea = null;
			if(oConvertUtils.isNotEmpty(position.getProvinceId())){
				provinceArea=areaService.getById(position.getProvinceId());
			}
			if(oConvertUtils.isNotEmpty(position.getEnterpriseId())){
				HTMerchant htMerchant = ihtMerchantService.getById(position.getEnterpriseId());
				if (htMerchant != null) {
					position.setEnterpriseName(htMerchant.getEnterpriseName());
				}
			}
			HTArea cityArea = null;
			if(oConvertUtils.isNotEmpty(position.getCityId())) {
				cityArea = areaService.getById(position.getCityId());
			}
			HTArea districtArea = null;
			if(oConvertUtils.isNotEmpty(position.getDistrictId())){
				districtArea = areaService.getById(position.getDistrictId());
			}

			if(oConvertUtils.isNotEmpty(provinceArea) && oConvertUtils.isNotEmpty(provinceArea.getName())){
				position.setProvinceName(provinceArea.getName());
			}

			if (oConvertUtils.isNotEmpty(cityArea) && oConvertUtils.isNotEmpty(cityArea.getName())){
				position.setCityName(cityArea.getName());
			}
			if(oConvertUtils.isNotEmpty(districtArea) && oConvertUtils.isNotEmpty(districtArea.getName())){
				position.setDistrictName(districtArea.getName());
			}

		}
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param hTPosition
	 * @return
	 */
	@AutoLog(value = "ht_position-添加")
	@ApiOperation(value="ht_position-添加", notes="ht_position-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HTPosition hTPosition) {
		hTPositionService.save(hTPosition);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param hTPosition
	 * @return
	 */
	@AutoLog(value = "ht_position-编辑")
	@ApiOperation(value="ht_position-编辑", notes="ht_position-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HTPosition hTPosition) {
		hTPositionService.updateById(hTPosition);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_position-通过id删除")
	@ApiOperation(value="ht_position-通过id删除", notes="ht_position-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hTPositionService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "ht_position-批量删除")
	@ApiOperation(value="ht_position-批量删除", notes="ht_position-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.hTPositionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "ht_position-通过id查询")
	@ApiOperation(value="ht_position-通过id查询", notes="ht_position-通过id查询")
	@RequestMapping(value = "/queryById", method = {RequestMethod.GET,RequestMethod.POST})
	public Result<?> queryById(@RequestParam(name="id",required=false) String id,  HttpServletRequest req) {
		if (req.getMethod().equals("POST")){
			String line = RequestUtil.getPostParam(req);
			if (oConvertUtils.isNotEmpty(line)) {
				HTPosition hTPosition  = JSONObject.parseObject(line, HTPosition.class);
				id = hTPosition.getId();
			}
		}

//		HTPosition position = hTPositionService.getById(id);
//		HTPositionCategory category = positionCategoryService.getById(position.getCategoryId());
//		position.setCategoryName(category.getName());
//
//		HTArea provinceArea = areaService.getById(position.getProvinceId());
//		HTArea cityArea = areaService.getById(position.getCityId());
//		HTArea districtArea = areaService.getById(position.getDistrictId());
//
//		position.setProvinceName(provinceArea.getName());
//		position.setCityName(cityArea.getName());
//		position.setDistrictName(districtArea.getName());

		HTPosition position = hTPositionService.getById(id);
		HTPositionCategory category = positionCategoryService.getById(position.getCategoryId());
		if(oConvertUtils.isNotEmpty(category) && oConvertUtils.isNotEmpty(category.getName( ))){
			position.setCategoryName(category.getName());
		}
		if(oConvertUtils.isNotEmpty(position.getEnterpriseId())){
			HTMerchant htMerchant = ihtMerchantService.getById(position.getEnterpriseId());
			if (htMerchant != null) {
				position.setEnterpriseName(htMerchant.getEnterpriseName());
			}
		}
		HTArea provinceArea = null;
		if(oConvertUtils.isNotEmpty(position.getProvinceId())){
			provinceArea=areaService.getById(position.getProvinceId());
		}
		HTArea cityArea = null;
		if(oConvertUtils.isNotEmpty(position.getCityId())) {
			cityArea = areaService.getById(position.getCityId());
		}
		HTArea districtArea = null;
		if(oConvertUtils.isNotEmpty(position.getDistrictId())){
			districtArea = areaService.getById(position.getDistrictId());
		}

		if(oConvertUtils.isNotEmpty(provinceArea) && oConvertUtils.isNotEmpty(provinceArea.getName())){
			position.setProvinceName(provinceArea.getName());
		}

		if (oConvertUtils.isNotEmpty(cityArea) && oConvertUtils.isNotEmpty(cityArea.getName())){
			position.setCityName(cityArea.getName());
		}
		if(oConvertUtils.isNotEmpty(districtArea) && oConvertUtils.isNotEmpty(districtArea.getName())){
			position.setDistrictName(districtArea.getName());
		}

		if(position==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(position);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param hTPosition
	 */
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, HTPosition hTPosition) {
		return super.exportXls(request, hTPosition, HTPosition.class, "ht_position");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, HTPosition.class);
	}

}
